<?php
	class newsControl extends commonControl{
	// 文章页面显示；
		function index(){
			$db = K("newsview");
			/*下面四行代码是看是否获得catid 和 uid 的，当获得到进行赋值；*/
			$catid = isset($_GET['catid']) ? (int)$_GET['catid'] : false;
        	$uid = isset($_GET['uid']) ? (int)$_GET['uid'] : false;
        	$where_catid = $catid ? "news.catid = $catid" : '';
       		$where_uid = $uid?"news.uid=$uid":'';
       		$count = $db -> where($where_catid) -> where($where_uid) ->where("recycled=0")->count();
       		$page = new page($count,4);
       		$news = $db->field("id,news.uid,username,title,click,news.catid,cname,addtime")->
                where($where_catid)->where($where_uid)->where("recycled=0")
                ->order("id desc")->limit($page->limit())->all();
       		$this->assign("news", $news);
        	$this->assign("page", $page->show());
			$this->display();
		}
	//显示添加文章页面；
		function add_show(){
		//连接来源；	
			$db = M("source");
			$res = $db -> field("sname,url") -> findall();
			$this->assign("res",$res);
		//连接栏目；
			if (!isset($_GET['catid'])) {
            	$catdb = M("category");
            	$category = $catdb->field("catid,cname,pid")->all();
            	$category = arr::channel($category, 'catid', 'pid', 0,0,2);
           	 	$this->assign("category", $category);
        	}
			$this->display();
		}
	//添加文章；
		function add(){
			$db = K("newsrelation");
			$_POST["addtime"]=time();
			$_POST["updatetime"]=time();
			$_POST['uid']=$_SESSION['user'];
			$_POST['thumb'] = isset($_POST['thumb'][1][0]) ? $_POST['thumb'][1][0] : '';
			if($db->add()){
				$this->success("发表文章成功","index");
			}else{
				$this->success("发表文章失败","index");
			}
		}
	//修改文章界面 
		function edit_show(){
			$id = $_GET["id"];        	
			$dbs = M("source");
			$res = $dbs -> field("sname,url") -> findall();
			$this->assign("res",$res);
			$db = K("newsview");
			$news = $db->join("category,news_data")->find($id);
        	$this->assign("field", $news);
        	$category = $db->join()->table("category")->field("catid,cname,pid")->all();
       		$category = arr::channel($category, 'catid', 'pid',0, 0, 2);
        	$this->assign("category", $category);
        	$this->display();
		}
	//修改文章
	    function edit() {
	        $db = K("newsrelation");
	        $_POST['updatetime'] = time();
	        $_POST['thumb'] = $_POST['thumb'][1][0];
	        if ($db->save()) {
	            $this->success("文章修改成功! ", __CONTROL__);
	        } else {
	            $this->error("文章修改失败");
	        }
	    }
	//删除
		function del(){
        	$id = $_GET['id'];
        	$db = K("newsrelation");
        	if(!C("recycled")){
          		if($db->update(array("id" => $id, "recycled" => 1))){
                	$this->success("文章放入回收站","index");
           		}
        	}else{
            	if($db->join("news_data")->del($id)){
               		$this->success("文章已经彻底删除","index");
            	}   
        	} 
        	$this->error("删除文章失败");
		}
	//放入回收站；
		function recycled(){
			$db = K('newsview');
        	$count = $db->where("recycled=1")->count();
       		$page = new page($count,4);
        	$news = $db->join("category")->field("id,title,click,news.catid,cname,addtime")->
                        where("recycled=1")->limit($page->limit())->all();
        	$this->assign("news", $news);
        	$this->assign("page", $page->show());
        	$this->display();
		}
	//彻底删除；
		function del_news() {
        	$id = $_GET['id'];
        	$news_db = K("newsrelation");
        	if ($news_db->join("news_data")->del($id)) {
            	$this->success("删除成功");
        	}else{
            	$this->del("删除文章失败");
        	}
    	}
    //还原文章；
    	function restore(){
    		$id = $_GET["id"];
    		$db = M("news");
        	if ($db->save(array("id" => $id, "recycled" => 0))) {
            	$this->success("文章已还原");
        	} else {
            	$this->error("还原文章失败");
        	}
    	}
    //将多个文件同时放入回收站中；
    	function delall(){
    		$keys = array_keys($_POST["del"]);
    		$db = M("news");
    		if($db->in($keys)->save(array("recycled"=>1))){
    			$this->success("放入回收站成功","index");
    		}else{
    			$this->error("放入回收站失败","index");
    		}
    	}
    //将多个文件同时彻底删除；
    	function clearall(){
    		$keys = array_keys($_POST["del"]);
    		$db = M("news");
    		if($db->in($keys)->del()){
    			$this->success("已彻底删除成功","index");
    		}else{
    			$this->error("彻底删除失败","index");
    		}
    	}

	}
?>